package com.example.autoboot.custom;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Region;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class CircleHoleView extends View {
    private Paint paint;
    private Path clipPath;
    private RectF bounds = new RectF();

    public CircleHoleView(Context context) {
        super(context);
        init();
    }

    public CircleHoleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CircleHoleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        clipPath = new Path();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 获取View的宽高
        int width = getWidth();
        int height = getHeight();

        // 计算圆心和半径，使圆居中
        int radius = Math.min(width, height) / 2;
        int centerX = width / 2;
        int centerY = height / 2;

        // 创建一个圆形的Path
        clipPath.reset();
        clipPath.addCircle(centerX, centerY, radius, Path.Direction.CW);

        // 使用clipPath裁剪画布
        canvas.save();
        canvas.clipPath(clipPath, Region.Op.DIFFERENCE); // 这里使用DIFFERENCE操作挖空圆形区域
        // 在这里可以绘制背景或其他你想在圆圈周围显示的内容
        canvas.drawColor(Color.BLUE); // 例如，绘制蓝色背景以示区别
        canvas.restore();

        // 重新绘制圆形内部，使其透明，显示底层视图
        paint.setColor(Color.TRANSPARENT);
        canvas.drawCircle(centerX, centerY, radius, paint);
    }
}